//
//  FileUtils.m
//  ConfuseTooles
//
//  Created by vincent on 2018/5/7.
//  Copyright © 2018年 vincent. All rights reserved.
//

#import "FileUtils.h"
#include  <mach-o/dyld.h>

@implementation FileUtils

+(NSString *) getCurrPath {
  return [[NSFileManager defaultManager] currentDirectoryPath];
}

+(BOOL) isExist:(NSString *)filePath {
  return (access([filePath UTF8String],0) == 0);
}

+(BOOL) createFile:(NSString *)filePath content:(NSData *)content {
  NSFileManager *manager = [NSFileManager defaultManager];
  if (![self isExist:filePath]) {
    NSString *path = [filePath stringByDeletingLastPathComponent];
    if (![manager fileExistsAtPath:path]) {
      [manager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
    }
    [manager createFileAtPath:filePath contents:content attributes:nil];
  } else {
    if ([manager isWritableFileAtPath:filePath])
    {
      [self append:content toFile:filePath];
    } else {
      return false;
    }
  }
  return true;
}

+(void) append:(NSData *)content toFile:(NSString *) filePath {
  NSFileHandle  *outFile;
  outFile = [NSFileHandle fileHandleForWritingAtPath:filePath];
  if(outFile == nil)
  {
    NSLog(@"Open of file for writing failed");
    return;
  }
  //找到并定位到outFile的末尾位置(在此后追加文件)
  [outFile seekToEndOfFile];
  //读取inFile并且将其内容写到outFile中
  [outFile writeData:content];
  //关闭读写文件
  [outFile closeFile];
}



@end
